/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = frstat_conns_add_conn_ct.c                         */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/12 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frexp_comm.h"
#include "frstat_conn.h"

extern int frstat_conns_add_conn_ct(Conntrack *p_st_ct, int flg_report_conn_info
                           , u_int ui_src_ip, u_int ui_dst_ip, u_short us_dst_port
                           , int i_conns, unsigned int ui_tx_bytes, unsigned int ui_rx_bytes);


static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    u_int ui_sip = iptools_parse_ipaddr("192.168.1.1");
    u_int ui_dip = iptools_parse_ipaddr("192.168.0.1");
    u_short us_dport = 80;
    
    frstat_conns_init(1000);
    
    i_ret = frstat_conns_add_conn_ct(NULL, FR_NO, ui_sip, ui_dip, us_dport, 1, 1, 1);
    CU_ASSERT_EQUAL(i_ret, FR_FAIL);
    frstat_conns_destroy();

    return;
}

static void case2(void)
{
    int i_ret = 0;
    Conntrack *p_st_ct = NULL;
    u_int ui_sip = iptools_parse_ipaddr("192.168.1.1");
    u_int ui_dip = iptools_parse_ipaddr("192.168.0.1");
    u_short us_dport = 80;
    
    frstat_conns_init(1000);
    
    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_ct == NULL)
        return;
    memset(p_st_ct, 0x00, sizeof(Conntrack));
    p_st_ct->flg_report_conn_info = FR_YES;

    i_ret = frstat_conns_add_conn_ct(p_st_ct, FR_NO, ui_sip, ui_dip, us_dport, 1, 1, 1);
    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(p_st_ct->report_src_ip, 0);
    CU_ASSERT_EQUAL(p_st_ct->report_dst_ip, 0);
    CU_ASSERT_EQUAL(p_st_ct->report_dst_port, 0);
    
    frfree(p_st_ct);
    frstat_conns_destroy();

    return;
}

static void case3(void)
{
    int i_ret = 0;
    Conntrack *p_st_ct = NULL;
    u_int ui_sip = iptools_parse_ipaddr("192.168.1.1");
    u_int ui_dip = iptools_parse_ipaddr("192.168.0.1");
    u_short us_dport = 80;

    frstat_conns_init(1000);
    
    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_ct == NULL)
        return;
    memset(p_st_ct, 0x00, sizeof(Conntrack));
    p_st_ct->flg_report_conn_info = FR_NO;

    i_ret = frstat_conns_add_conn_ct(p_st_ct, FR_YES, ui_sip, ui_dip, us_dport, 1, 1, 1);
    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(p_st_ct->report_src_ip, 16885952);
    CU_ASSERT_EQUAL(p_st_ct->report_dst_ip, 16820416);
    CU_ASSERT_EQUAL(p_st_ct->report_dst_port, 80);
    
    frfree(p_st_ct);
    frstat_conns_destroy();

    return;
}

static void case4(void)
{
    int i_ret = 0;
    Conntrack *p_st_ct = NULL;
    u_int ui_sip = iptools_parse_ipaddr("192.168.1.1");
    u_int ui_dip = iptools_parse_ipaddr("192.168.0.1");
    u_short us_dport = 80;

    frstat_conns_init(1000);

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_ct == NULL)
        return;
    memset(p_st_ct, 0x00, sizeof(Conntrack));
    p_st_ct->flg_report_conn_info = FR_YES;

    i_ret = frstat_conns_add_conn_ct(p_st_ct, FR_NO, ui_sip, ui_dip, us_dport, 0, 1, 1);
    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    
    frfree(p_st_ct);
    frstat_conns_destroy();
    
    return;
}

int add_suite_frstat_conns_add_conn_ct(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("framework frstat_conns_add_conn_ct()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
